import {createRouter,createWebHistory} from 'vue-router'
//- 异步加载组件
import {defineAsyncComponent} from 'vue'
//- 路由规则
const myRouter = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/login',
        name: "Login",
        component: defineAsyncComponent(()=>import('../components/Login.vue'))
    },
    {
        //商品首页
        path: '/shophome',
        name: "Shophome",
        component: defineAsyncComponent(()=>import('../components/ShopHome.vue'))
    },
    {
        path: '/ShoppingCart',
        name: "ShoppingCart",
        component: defineAsyncComponent(()=>import('../components/ShoppingCart.vue'))
    },
    {
        path: '/Order',
        name: "MyOrder",
        component: defineAsyncComponent(()=>import('../components/MyOrder.vue'))
    },
    {
        path: '/collect',
        name: "Collect",
        component: defineAsyncComponent(()=>import('../components/Collect.vue'))
    },
    {
        path: '/Wallet',
        name: "Wallet",
        component: defineAsyncComponent(()=>import('../components/Wallet.vue'))
    },
    {
        path: '/register',
        name: "Register",
        component: defineAsyncComponent(()=>import('../components/Register.vue'))
    },
    {
        path: '/maintenance',
        name: "Maintenance",
        component: defineAsyncComponent(()=>import('../components/Maintenance.vue'))
    },
    {
        path: '/announcement-management',
        name: "Announcementmanagement",
        component: defineAsyncComponent(()=>import('../components/Announcement_Management.vue'))
    },
    {
        path: '/visitor',
        name: "Visitor",
        component: defineAsyncComponent(()=>import('../components/Visitor.vue'))
    },
    {
        path: '/complaints',
        name: "Complaints",
        component: defineAsyncComponent(()=>import('../components/Complaints.vue'))
    },
    {
        path: '/stratafees',
        name: "Stratafees",
        component: defineAsyncComponent(()=>import('../components/Stratafees.vue'))
    },
    {
        path: '/payment-management',
        name: "Paymentmanagement",
        component: defineAsyncComponent(()=>import('../components/Payment_Management.vue'))
    },
    {
        path: '/complaints-management',
        name: "Complaintsmanagement",
        component: defineAsyncComponent(()=>import('../components/Complaints_Management.vue'))
    },
    {
        path: '/park-management',
        name: "Parkmanagement",
        component: defineAsyncComponent(()=>import('../components/Park_Management.vue'))
    },
    {
        path: '/visitor-management',
        name: "Visitormanagement",
        component: defineAsyncComponent(()=>import('../components/Visitor_Management.vue'))
    },

    {
        path: '/goods-Management',
        name: "Goods Management",
        component: defineAsyncComponent(()=>import('../components/Goods_Management.vue'))
    },
    {
        path: '/visitorrank-management',
        name: "Visitor Ranking",
        component: defineAsyncComponent(()=>import('../components/VisitorRanking_Management.vue'))
    },
    {
        path: '/rank-management',
        name: "Ranking",
        component: defineAsyncComponent(()=>import('../components/Ranking_Management.vue'))
    },
    {
        path: '/park',
        name: "Park",
        component: defineAsyncComponent(()=>import('../components/Park.vue'))
    },
    {
        path: '/home',
        name: "Home",
        component: defineAsyncComponent(()=>import('../components/Home.vue'))
    },
    {
        path: '/announcement',
        name: "Announcement",
        component: defineAsyncComponent(()=>import('../components/Announcement.vue'))
    },
    {
        path: '/area-management',
        name: "Area",
        component: defineAsyncComponent(()=>import('../components/Area_Management.vue'))
    },
    {
        path: '/shop-management',
        name: "Shop",
        component: defineAsyncComponent(()=>import('../components/Shop_Management.vue'))
    },
    {
        path: '/order-management',
        name: "Order",
        component: defineAsyncComponent(()=>import('../components/Order_Management.vue'))
    }
    ,
    {
        path: '/maintenance-management',
        name: "Maintenance management",
        component: defineAsyncComponent(()=>import('../components/Maintenance_Management.vue'))
    },
    {
        path: '/admin',
        name: "Admin",
        component: defineAsyncComponent(()=>import('../components/Admin.vue')),
        redirect:'/admin/adminhomepage',
        children:[
            {
                path:'/admin/adminhomepage',
                name: 'adminhomepage',
                component: ()=>import('../components/AdminHomepage.vue')
            },
            {
                path:'/admin/role-management',
                name: 'role-management',
                component: ()=>import('../components/Role-management.vue')
            },
            {
                path:'/admin/user-management',
                name: 'user-management',
                component: ()=>import('../components/User-management.vue')
            },
            {
                path:'/admin/membership-management',
                name: 'membership-management',
                component: ()=>import('../components/Membership-Management.vue')
            },
            {
                path:'/admin/userloginlogs',
                name: 'userloginlogs',
                component: ()=>import('../components/Userloginlogs.vue')
            },
            {
                path:'/admin/adminloginlogs',
                name: 'adminloginlogs',
                component: ()=>import('../components/Adminloginlogs.vue')
            },
        ]
    }


]
//- 创建路由对象
const router = createRouter({
    history: createWebHistory(),
    routes: myRouter
})
//全局守卫  访问非Login界面时，验证是否已登录
// router.beforeEach((to,from,next)=>{
//     //判断是否已登录 查sessionStorage中是否有isAuthenticated信息
//     let isAuthenticated = sessionStorage.getItem("isAuthenticated")
//     //判断路由的别名不是登录且未进行登录认证，就跳转去登录
//     if(to.name!="Login1"&&!isAuthenticated){
//         next({name: "Login1"})
//     }else if(to.name=="Login1"&&isAuthenticated){//已登录，不允许退回到登录页面
//         next({name:"Home"})
//     }else{
//         next()
//     }
// })
//-将路由暴露出去，其他文件才能访问
export default router